package dynamic;

import java.util.ArrayList;
import java.util.List;

public class CompareGene
{
    public String gene1;
    public String gene2;
    public int[][] arrayCourse;
    public String cGene1;//变更后的DNA1
    public String cGene2;//变更后的DNA2

    public CompareGene(String gene1, String gene2)
    {
        this.gene1 = gene1;
        this.gene2 = gene2;
        arrayCourse = new int[gene1.length() + 1][gene2.length() + 1];
        Fill_arrayCourse();
        LoadDNA();
    }
    //基因字母（其中一个可为空格"_"）比较得到的的分数
    public static int GetCourse(String gene1, String gene2) {
        if (gene1.equals(gene2))
            return 5;
        else if ((gene1.equals("A") && gene2.equals("C")) || (gene1.equals("C") && gene2.equals("A")))//A
            return -1;
        else if ((gene1.equals("A") && gene2.equals("G")) || (gene1.equals("G") && gene2.equals("A")))
            return -2;
        else if ((gene1.equals("A") && gene2.equals("T")) || (gene1.equals("T") && gene2.equals("A")))
            return -1;
        else if ((gene1.equals("A") && gene2.equals("_")) || (gene1.equals("_") && gene2.equals("A")))
            return -3;
        else if ((gene1.equals("C") && gene2.equals("G")) || (gene1.equals("G") && gene2.equals("C")))//C
            return -3;
        else if ((gene1.equals("C") && gene2.equals("T")) || (gene1.equals("T") && gene2.equals("C")))
            return -2;
        else if ((gene1.equals("C") && gene2.equals("_")) || (gene1.equals("_") && gene2.equals("C")))
            return -4;
        else if ((gene1.equals("G") && gene2.equals("T")) || (gene1.equals("T") && gene2.equals("G")))//G
            return -2;
        else if ((gene1.equals("G") && gene2.equals("_")) || (gene1.equals("_") && gene2.equals("G")))
            return -2;
        else if ((gene1.equals("T") && gene2.equals("_")) || (gene1.equals("_") && gene2.equals("T")))//G
            return -1;

        return 0;
    }
    public static boolean checkLetter(String string) {//判断输入的基因序列是不是由ACGT组成的

        List<String> geneLetter = new ArrayList<String>();
        geneLetter.add("A");
        geneLetter.add("C");
        geneLetter.add("G");
        geneLetter.add("T");
        for(int i = 0;i < string.length();i++) {
            String s = string.charAt(i)+"";
            if(!geneLetter.contains(s)) {
                return false;
            }
        }
        return true;
    }
    //填充arrayCourse表格,,比较2个基因序列
    public void Fill_arrayCourse()
    {
        for (int i = 1; i < arrayCourse.length; i++)
            arrayCourse[i][0] = arrayCourse[i - 1][0] + GetCourse(gene1.substring(i - 1, i), "_");
        for (int i = 1; i < arrayCourse[0].length; i++)
            arrayCourse[0][i] = arrayCourse[0][i - 1] +  GetCourse("_", gene2.substring(i - 1, i));
        for (int i = 1; i < arrayCourse.length; i++)
        {
            for (int j = 1; j < arrayCourse[0].length; j++)
            {
                int v1 = 0, v2 = 0, v3 = 0;
                //情况1：gene1和"_"搭配
                v1 = arrayCourse[i - 1][j] + GetCourse(gene1.substring(i - 1, i), "_");

                //情况2：gene1和DNA2搭配
                v2 = arrayCourse[i - 1][j - 1] + GetCourse(gene1.substring(i - 1,i), gene2.substring(j - 1, j));
                //情况3："_"和DNA2搭配
                v3 = arrayCourse[i][j - 1] + GetCourse("_" , gene2.substring(j - 1, j));
                arrayCourse[i][j] = GetMaxNum(v1, v2, v3);
            }
        }
    }

    //获取变更后的gene1和DNA2
    private void LoadDNA()
    {
        cGene1 = "";
        cGene2 = "";
        int rowIndex = arrayCourse.length - 1;//行
        int fieldIndex = arrayCourse[0].length - 1;//列
        while (rowIndex != 0 || fieldIndex != 0)
        {
            if (rowIndex == 0)//当行等于0时，cGene1已经匹配完成，cGene2前面的剩余基因与空格匹配
            {
                for (int i = 0; i < fieldIndex; i++)
                    cGene1 = "_" + cGene1;//匹配后的cGene1
                cGene2 = gene2.substring(0, fieldIndex) + cGene2;//匹配后的cGene2
                return;
            }
            if (fieldIndex == 0)//列为cGene2 同行一样
            {
                for (int i = 0; i < rowIndex; i++)
                    cGene2 = "_" + cGene2;
                cGene1 = gene1.substring(0, rowIndex) + cGene1;
                return;
            }

            int top = arrayCourse[rowIndex - 1][fieldIndex] +GetCourse(gene1.substring(rowIndex - 1, rowIndex), "_");//DNA1的最后一个基因和空格匹配
            int left = arrayCourse[rowIndex][fieldIndex - 1] + GetCourse(gene2.substring(fieldIndex - 1, fieldIndex), "_");//DNA2的最后一个基因和空格匹配
            int both = arrayCourse[rowIndex - 1][fieldIndex - 1] + GetCourse(gene1.substring(rowIndex - 1, rowIndex), gene2.substring(fieldIndex - 1, fieldIndex));
            //DNA1的最后一个基因和DNA最后一个基因匹配
            int value = arrayCourse[rowIndex][fieldIndex];
            if (value == top)
            {
                cGene1 = gene1.substring(rowIndex - 1, rowIndex) + cGene1;
                cGene2 = "_" + cGene2;
                rowIndex = rowIndex - 1;
            }
            else if (value == left)
            {
                cGene1 = "_" + cGene1;
                cGene2 = gene2.substring(fieldIndex - 1, fieldIndex) + cGene2;
                fieldIndex = fieldIndex - 1;
            }
            else
            {
                cGene1 = gene1.substring(rowIndex - 1, rowIndex) + cGene1;
                cGene2 = gene2.substring(fieldIndex - 1, fieldIndex) + cGene2;
                rowIndex = rowIndex - 1;
                fieldIndex = fieldIndex - 1;
            }
        }
    }

    public int GetResult(){
        int i = arrayCourse.length - 1;
        int j = arrayCourse[0].length - 1;
        return arrayCourse[i][j];
    }

    //获取3个数中的最大项
    private int GetMaxNum(int a, int b, int c)
    {
        int max = a;
        max = b > max ? b : max;
        max = c > max ? c : max;
        return max;
    }
}
